<?php
/**
 * Created by PhpStorm.
 * User: yangzhic
 * Date: 2017/3/1
 * Time: 16:08
 */

namespace SDXApi\Controller;


use SDXApi\Service\Wechat;
use Think\Controller;

class WechatController extends AppController
{
    private $weObj;
    private $options = array();

    protected function _initialize(){
        parent::_initialize($apptoken, $apptime,true);
        $wechat_id = I('post.wechat_id',1,'int');
        $wechat = S('wechat_setting_'.$wechat_id);
        if(empty($wechat)){
            $wechat = M('wechat')->where(array('default_wx'=>$wechat_id))->find();
            S('wechat_setting_'.$wechat_id,$wechat,3600);
        }

        $this->options['token']= $wechat['token'];
        $this->options['appid']= $wechat['appid'];
        $this->options['appsecret']= $wechat['appsecret'];

        $this->weObj = new Wechat($this->options);
    }

    /**
     * 获取js
     */
    public function getJsSign(){
        $url = I('post.url');
        if(empty($url)){
            returnApiError('url不能为空');
        }

        $signPackage = $this->weObj->getJsSign($url);

        if($signPackage){
            returnApiSuccess($signPackage);
        }else{
            returnApiError('获取签名失败');
        }
    }
}